<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button 
        class="filter-item"
        type="danger" 
        icon="el-icon-delete"
        @click="handleStatusChange(1)"
      >批量冻结</el-button>
      <el-button 
        class="filter-item"
        type="success" 
        icon="el-icon-delete"
        @click="handleStatusChange(0)"
      >批量恢复</el-button>
      <el-dropdown 
        class="filter-item" 
        style="margin-left: 10px" 
        trigger="click" 
        placement="bottom" 
        @command="handleRoleChange"
      >
        <el-button type="primary">批量设置角色</el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item 
            v-for="(item, index) in roleList" 
            :key="item.id" 
            :command="index" 
            :disabled="item.is_super == 1" 
          >{{ item.name }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-select
        class="filter-item" 
        style="margin-left: 10px" 
        size="medium" 
        v-model="listQuery.role_id"
        clearable 
        placeholder="角色筛选" 
        :loading="roleLoaidng"
        @clear="listQuery.role_id = null" 
        @change="handleFilter" 
      >
        <el-option
          v-for="item in roleList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        class="filter-item" 
        style="margin-left: 10px"
        size="medium" 
        v-model="listQuery.status"
        clearable 
        placeholder="账号状态筛选" 
        @clear="listQuery.status = null"
        @change="handleFilter" 
      >
        <el-option
          v-for="item in statusList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-input
        class="filter-item" 
        style="width: 240px; margin-left: 10px" 
        size="medium" 
        v-model="listQuery.keyword"
        placeholder="账号/绑定手机/昵称 筛选"
        prefix-icon="el-icon-search"
        clearable 
        @clear="handleFilter" 
        @keyup.enter.native="handleFilter"
      />
    </div>
    <el-table 
      style="width: 100%;" 
      ref="listTable" 
      v-loading="listLoading"
      :data="listData" 
      row-key="id" 
      border 
      highlight-current-row 
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" :selectable="handleSelectable" />

      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色" width="120">
        <template slot-scope="scope">
          {{ scope.row.role.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="头像" width="100">
        <template slot-scope="{row}">
          <el-avatar shape="square" :src="row.image ? row.image.url : ''"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="账号" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="绑定手机" width="120">
        <template slot-scope="scope">
          {{ scope.row.bind_phone }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="登录次数" width="100">
        <template slot-scope="scope">
          {{ scope.row.login_count }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后登录ip" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.login_ip }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后登录时间" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.login_time }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="注册时间" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.create_time }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否冻结" width="80">
        <template slot-scope="{row}">
          <el-tag
            effect="plain"
            :type="row.status | statusFilter(2)"
          >{{ row.status | statusFilter(1) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150">
        <template slot-scope="{row}" v-if="!isSuper(row.role.is_super)">
          <el-button type="primary" size="small" @click="handleEdit(row.id)">编辑</el-button>
          <el-button 
            :type="row.status | statusBtnFilter(2)" 
            size="small" 
            @click="handleStatus(row.id, row.status == 1 ? 0 : 1)"
          >
            {{ row.status | statusBtnFilter(1) }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { managerList, managerChangeStatus, managerSetRole } from '@/api/manager'
import { roleList } from '@/api/role'
import { isSuper } from '@/utils/util'

export default {
  name: 'ManagerList',
  components: { Pagination },
  filters: {
    statusFilter(val, type = 1) {
      if (val == 1) {
        return type == 1 ? '冻结' : 'danger'
      } else {
        return type == 1 ? '正常' : 'success'
      }
    },
    statusBtnFilter(val, type = 1) {
      if (val == 1) {
        return type == 1 ? '恢复' : 'success'
      } else {
        return type == 1 ? '冻结' : 'danger'
      }
    }
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        role_id: null,
        status: null,
        keyword: ''
      },
      total: 0,
      listLoading: false,
      listData: [],
      roleLoaidng: false,
      roleList: [],
      statusList: [
        { id: 0, name: '正常' },
        { id: 1, name: '冻结' },
      ]
    }
  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    isSuper,
    // 获取列表
    async getList() {
      try {
        this.listLoading = true
        const { page } = await managerList(this.listQuery)
        this.listData = page.data
        this.total = page.total
      } finally {
        this.listLoading = false
      }
    },
    // 获取角色列表
    async getRoleList() {
      try {
        this.roleLoaidng = true
        const { data } = await roleList()
        this.roleList = data
      } finally {
        this.roleLoaidng = false
      }
    },
    // 筛选
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 编辑
    handleEdit(id) {
      this.$router.push({ name: 'ManagerUpdate', params: { id } });
    },
    // 设置改行知否可被选中
    handleSelectable(row, index) {
      return !row.role.is_super
    },
    // 多选
    handleSelectionChange(val) {
      this.selectionItems = val
    },
    // 冻结/恢复选中
    handleStatusChange(status) {
      if (this.selectionItems && this.selectionItems.length) {
        const managerIds = this.selectionItems.map(v => v.id).join()
        this.handleStatus(managerIds, status).then(() => {
          this.$refs.listTable.clearSelection()
        })
      } else {
        this.$notify.warning({
          title: '警告',
          message: '请至少选择一项',
          duration: 3000,
        })
      }
    },
    // 批量冻结/恢复
    async handleStatus(id, status) {
      await this.$confirm("确定要操作【冻结/恢复】么？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      await managerChangeStatus({ ids: id, status: status })
      this.$notify.success({
        title: '成功',
        message: '操作成功',
        duration: 3000,
      });
      this.getList()
    },
    // 批量设置角色
    async handleRoleChange(roleIndex) {
      if (this.selectionItems && this.selectionItems.length) {
        const currentRole = this.roleList[roleIndex]
        await this.$confirm(`确定为选中用户分配【${currentRole.name}】角色么？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        const managerIds = this.selectionItems.map(v => v.id).join()
        await managerSetRole({ ids: managerIds, role_id: currentRole.id })
        this.$notify.success({
          title: '成功',
          message: '角色分配成功',
          duration: 3000,
        });
        this.$refs.listTable.clearSelection()
        this.getList()
      } else {
        this.$notify.warning({
          title: '警告',
          message: '请至少选择一个用户',
          duration: 3000,
        })
      }
    }
  },
  
}
</script>

<style lang="scss" scoped>
  
</style>